﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace Green.Utility
{
    public class SerializerUtility
    {
        #region XmlSerializer
        public static string XmlSerializer<T>(T obj) where T : class, new()
        {
            return XmlSerializer<T>(obj, "");
        }

        public static string XmlSerializer(object obj)
        {
            System.Xml.Serialization.XmlSerializer serializer = null;

            serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);
            serializer.Serialize(sw, obj);
            sw.Close();
            return sb.ToString();
        }

        public static string XmlSerializer<T>(T obj, string defaultnamespace) where T : class, new()
        {
            System.Xml.Serialization.XmlSerializer serializer = null;
            if (!string.IsNullOrEmpty(defaultnamespace))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), defaultnamespace);
            }
            else
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            }
            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);
            serializer.Serialize(sw, obj);
            sw.Close();
            return sb.ToString();
        }

        public static string XmlSerializerToFile<T>(T obj, string defaultnamespace, string filepath) where T : class, new()
        {
            if (!System.IO.File.Exists(filepath))
            {
                System.IO.File.Create(filepath);
            }
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), defaultnamespace);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(filepath);
            serializer.Serialize(sw, obj);
            return System.IO.File.ReadAllText(filepath);
        }

        public static string XmlSerializerToFile<T>(T obj, string filepath) where T : class, new()
        {
            return XmlSerializerToFile(obj, filepath);
        }

        public static T XmlDeserialize<T>(string xmlstring) where T : class, new()
        {
            return XmlDeserialize<T>(xmlstring, "");
        }

        public static T XmlDeserialize<T>(string xmlstring, string defaultnamespace) where T : class, new()
        {

            System.Xml.Serialization.XmlSerializer serializer = null;
            if (!string.IsNullOrEmpty(defaultnamespace))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), defaultnamespace);
            }
            else
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            }

            System.IO.StringReader sr = new System.IO.StringReader(xmlstring);
            var obj = serializer.Deserialize(sr) as T;
            sr.Close();
            return obj;
        }

        public static T XmlDeserializeFromFile<T>(string filepath, string defaultnamespace) where T : class, new()
        {
            if (!System.IO.File.Exists(filepath))
            {
                throw new System.IO.FileNotFoundException("file not find!", filepath);
            }
            System.Xml.Serialization.XmlSerializer serializer = null;
            if (string.IsNullOrEmpty(defaultnamespace))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            }
            else
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), defaultnamespace);
            }

            System.IO.StreamReader sr = new System.IO.StreamReader(filepath);
            var obj = serializer.Deserialize(sr) as T;
            sr.Close();
            return obj;
        }

        public static T XmlDeserializeFromFile<T>(string filepath) where T : class, new()
        {

            return XmlDeserializeFromFile<T>(filepath, "");
        }
        #endregion

        #region BinarySerializer
        public static Byte[] BinarySerializer<T>(T obj) where T : class, new()
        {
            BinaryFormatter binFormatter = new BinaryFormatter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            binFormatter.Serialize(ms, obj);
            ms.Flush();
            ms.Close();
            return ms.ToArray();
        }
        public static void BinarySerializerToFile<T>(T obj, string file) where T : class, new()
        {

            BinaryFormatter binFormatter = new BinaryFormatter();
            var stream = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate);
            binFormatter.Serialize(stream, obj);
            stream.Flush();
            stream.Close();
        }

        public static T BinaryDeserialize<T>(byte[] bytes) where T : class, new()
        {
            BinaryFormatter binFormatter = new BinaryFormatter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
            var obj = binFormatter.Deserialize(ms) as T;
            ms.Flush();
            ms.Close();
            return obj;
        }
        public static T BinaryDeserializeFromFile<T>(string file) where T : class, new()
        {
            if (!System.IO.File.Exists(file))
            {
                throw new System.IO.FileNotFoundException("The file can't be found;");
            }

            BinaryFormatter binFormatter = new BinaryFormatter();
            var stream = new System.IO.FileStream(file, System.IO.FileMode.Open);
            var obj = binFormatter.Deserialize(stream) as T;
            stream.Flush();
            stream.Close();
            return obj;
        }
        #endregion

        #region Json
        public static string JsonSerializer<T>(T obj) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();


            return strJSON;
        }

        public static string JsonSerializer(object obj)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();


            return strJSON;
        }

        public static string JsonSerializer<T>(T obj, Encoding encoding) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = encoding.GetString(ms.ToArray());
            ms.Close();
            return strJSON;
        }

        public static string JsonSerializer<T>(T obj, string encoding) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.GetEncoding(encoding).GetString(ms.ToArray());
            ms.Close();
            return strJSON;
        }

        public static T JsonDeserialize<T>(string sJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);
            ms.Flush();
            ms.Close();
            return obj;
        }

        public static T JsonDeserialize<T>(string sJson, Encoding encoding) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(encoding.GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);
            ms.Flush();
            ms.Close();
            return obj;
        }

        public static T JsonDeserialize<T>(string sJson, string encoding) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.GetEncoding(encoding).GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);
            ms.Flush();
            ms.Close();
            return obj;
        }
        #endregion

        #region SOAPSerialize
        public static void SOAPSerializeToFile<T>(T obj, string file)
        {
            using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write))
            {
                SoapFormatter formater = new SoapFormatter();
                formater.Serialize(stream, obj);
                stream.Flush();
                stream.Close();
            }
        }

        public static string SOAPSerialize<T>(T obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                SoapFormatter formater = new SoapFormatter();
                formater.Serialize(ms, obj);
                ms.Flush();
                ms.Close();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        public static T SOAPDeserializeFromFile<T>(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                SoapFormatter formater = new SoapFormatter();
                var obj = (T)formater.Deserialize(stream);
                stream.Flush();
                stream.Close();
                return obj;
            }
        }

        public static T SOAPDeserialize<T>(string sopstr) where T : class
        {
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sopstr)))
            {
                SoapFormatter formater = new SoapFormatter();
                var obj = (T)formater.Deserialize(ms);
                ms.Flush();
                ms.Close();
                return obj;
            }
        }
        #endregion
    }
}
